//
//  UIViewController+Load.swift
//  DigitalMuseum
//
//  Created by cheyongzi on 2023/12/14.
//

import UIKit

extension UIViewController{
    public class func loadXIB(_ name: String = "")-> UIViewController{
        var xibName = name
        if (xibName.isEmpty){
            xibName = String(describing: self)
        }
        return self.init(nibName: xibName, bundle: nil)
    }
}

extension UIAlertAction{
    convenience init(localized: String,style: UIAlertAction.Style = .default,handler:((UIAlertAction) -> Void)? = nil) {
        self.init(title: localized.localized,style: style,handler: handler)
    }
}

extension UIViewController{
    func showAlert(title: String?, message: String? = nil, actions: [UIAlertAction], cancel: Bool = true){
        let controller = UIAlertController(title: title?.localized, message: message?.localized, preferredStyle: .alert)
        if cancel{
            controller.addAction(UIAlertAction.init(localized: "取消", style: .cancel))
        }
        actions.forEach { action in
            controller.addAction(action)
        }
        self.present(controller, animated: true)
    }
}

extension UIViewController{
    private static let NavBackground = ObjectAssociation<UIColor>(.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
    @IBInspectable
    var navBackground: UIColor?{
        get{return UIViewController.NavBackground[self]}
        set{
            var naviBar: UINavigationBar?
            if self.isKind(of: UINavigationController.self){
                naviBar = (self as? UINavigationController)?.navigationBar
            } else {
                naviBar = self.navigationController?.navigationBar
            }
            if #available(iOS 13.0, *) {
                let navBarAppearance = UINavigationBarAppearance()
                navBarAppearance.backgroundColor = newValue
                navBarAppearance.shadowColor = UIColor.clear
                navBarAppearance.shadowImage = UIImage()
                naviBar?.standardAppearance = navBarAppearance
                naviBar?.scrollEdgeAppearance = navBarAppearance
            } else {
                naviBar?.backgroundColor = newValue
                naviBar?.shadowImage = UIImage()
            }
            UIViewController.NavBackground[self] = newValue
        }
    }
    
    private static let NaviImage = ObjectAssociation<String>(.OBJC_ASSOCIATION_COPY)
    @IBInspectable
    var naviImage: String{
        set{
            self.navigationItem.titleView = UIImageView(image: UIImage.init(named: newValue))
            UIViewController.NaviImage[self] = newValue
        }
        get{
            return UIViewController.NaviImage[self] ?? ""
        }
    }
    
    private static let NaviTitle = ObjectAssociation<String>(.OBJC_ASSOCIATION_COPY)
    @IBInspectable
    var naviTitle: String{
        set{
            let titleLabel = UILabel.init()
            titleLabel.textColor = UIColor.black
            titleLabel.font = UIFont.systemFont(ofSize: 17, weight: .semibold)
            titleLabel.text = newValue.localized
            self.navigationItem.titleView = titleLabel
            UIViewController.NaviTitle[self] = newValue
        }
        get{
            return UIViewController.NaviTitle[self] ?? ""
        }
    }
    
    @IBInspectable
    var titleColor: UIColor{
        set{
            if let titleLabel = self.navigationItem.titleView as? UILabel {
                titleLabel.textColor = newValue
            }
        }
        get{
            return UIColor.clear
        }
    }
}

extension UIViewController{
    @objc func dismissController(){
        if self.navigationController?.viewControllers.count == 1 {
            self.navigationController?.dismiss(animated: true)
        } else {
            self.navigationController?.popViewController(animated: true)
        }
    }
    
    func enableRightItem(enable: Bool,all:Bool = false){
        guard !all else {
            self.navigationItem.rightBarButtonItems?.forEach({ item in
                self.enableRightItem(item: item, enable: enable)
            })
            return
        }
        let item = self.navigationItem.rightBarButtonItem ?? self.navigationItem.rightBarButtonItems?.first
        self.enableRightItem(item: item, enable: enable)
    }
    
    private func enableRightItem(item: UIBarButtonItem?,enable:Bool){
        guard let button = item?.customView as? UIButton else {
            return
        }
        button.setTitleColor(enable ? UIColor.black:UIColor.black.withAlphaComponent(0.2), for: .normal)
    }
}
